var app = getApp()
Page({
    data: {
        code: 0,
        tab: 1,
        currentIndex: 0,
        swiperHeight: ''
    },

    onLoad(options) {
        var that = this
        if (options.scene || options.fuid) {
            const query = app.sceneToObject(options.scene)
            // console.log(query)
            that.setData({
                fuid: options.fuid || query.fuid
            })
        }

        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res
                },() => {
                    that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo
            },() => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const { uid,openid } = that.data.userinfo
        const data = { uid,openid }
        app.api_request('?c=home','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res
                })
                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },

    toSignUp(e) {
        const { url } = e.currentTarget.dataset
        if (url == '') {
            tt.showModal({
                //title: '提示',
                showCancel: false,
                content: '增驾此车型需前往蓝盾驾校报名',
            })
        } else {
            tt.navigateTo({
                url
            })
        }
    },

    setTab(e) {
        this.setData({
            tab: e.target.dataset.tab
        })
    },

    getheight: function (e) {
        var width = tt.getSystemInfoSync().windowWidth - 30
        var imgheight = e.detail.height
        var imgwidth = e.detail.width
        var height = width * imgheight / imgwidth + "px"
        this.setData({
            swiperHeight: height
        })
    },

    swiperChange: function (e) {
        let { source } = e.detail
        if (source === 'autoplay' || source === 'touch') {
            this.setData({
                currentIndex: e.detail.current
            })
        }
    },

    onShareAppMessage: function () {
        var that = this
        return {
            title: that.data.pagedata.appTitle,
            desc: that.data.pagedata.appDesc,
            path: '/pages/index/index?fuid=' + that.data.userinfo.uid
        }
    }
})